#include<sys/types.h>
#include<sys/wait.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

void work()
{
    int cnt = 5;
    while(cnt--)
    {
        printf("I am child process pid:%d  ppid:%d  cnt:%d\n",getpid(),getppid(),cnt);
        sleep(1);
    }
}

int main()
{
    pid_t id = fork();
    if(id<0)perror("fork"),exit(-1);
    else if(id==0)
    {
        //child
        work();
        printf("child process done\n");
        exit(1);
    }
    else
    {
        //father
        printf("I am father process pid:%d  ppid:%d\n",getpid(),getppid());
        printf("father process begin wait\n");
        // pid_t w_id = wait(NULL);
        sleep(6);
        int status;
        pid_t w_id = waitpid(-1,&status,WNOHANG);
        if(w_id==id)
        {            
            printf("child exit success, w_id:%d exit sig:%d, exit code:%d, core dump:%d\n",w_id,status&0x7f,(status>>8)&0xff,(status>>7)&1);
            if(WIFEXITED(status))
                printf("child process normal quit, exit code:%d\n",WEXITSTATUS(status));
            else if(WCOREDUMP(status))
                printf("core dump produce\n");
        }
    }

    // pid_t id = wait(NULL);
    // printf("pid:%d\n",id);

    return 0;
}